<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - sqlite_tools.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2011  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_SQLiTE_TOOLS_H_
<font color='#0000FF'>#define</font> DLIB_SQLiTE_TOOLS_H_


<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='sqlite_tools_abstract.h.html'>sqlite_tools_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='sqlite.h.html'>sqlite.h</a>"

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>namespace</font> dlib
<b>{</b>

    <font color='#0000FF'>class</font> <b><a name='transaction'></a>transaction</b> : noncopyable
    <b>{</b>
    <font color='#0000FF'>public</font>:
        <b><a name='transaction'></a>transaction</b> <font face='Lucida Console'>(</font>
            database<font color='#5555FF'>&amp;</font> db_
        <font face='Lucida Console'>)</font> :
            db<font face='Lucida Console'>(</font>db_<font face='Lucida Console'>)</font>,
            committed<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>
        <b>{</b>
            db.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>begin transaction</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>void</u></font> <b><a name='commit'></a>commit</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>committed<font face='Lucida Console'>)</font>
            <b>{</b>
                committed <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                db.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>commit</font>"<font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>

        ~<b><a name='transaction'></a>transaction</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b><a name='noexcept'></a>noexcept</b><font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>committed<font face='Lucida Console'>)</font>
                db.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>rollback</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#0000FF'>private</font>:
        database<font color='#5555FF'>&amp;</font> db;
        <font color='#0000FF'><u>bool</u></font> committed;

    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='query_object'></a>query_object</b> <font face='Lucida Console'>(</font>
        database<font color='#5555FF'>&amp;</font> db,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> query,
        T<font color='#5555FF'>&amp;</font> item
    <font face='Lucida Console'>)</font>
    <b>{</b>
        statement <font color='#BB00BB'>st</font><font face='Lucida Console'>(</font>db, query<font face='Lucida Console'>)</font>;
        st.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> st.<font color='#BB00BB'>get_num_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
        <b>{</b>
            st.<font color='#BB00BB'>get_column_as_object</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,item<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>inline</font> std::string <b><a name='query_text'></a>query_text</b> <font face='Lucida Console'>(</font>
        database<font color='#5555FF'>&amp;</font> db,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> query
    <font face='Lucida Console'>)</font>
    <b>{</b>
        statement <font color='#BB00BB'>st</font><font face='Lucida Console'>(</font>db, query<font face='Lucida Console'>)</font>;
        st.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> st.<font color='#BB00BB'>get_num_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> temp <font color='#5555FF'>=</font> st.<font color='#BB00BB'>get_column_as_text</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> temp;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>inline</font> <font color='#0000FF'><u>double</u></font> <b><a name='query_double'></a>query_double</b> <font face='Lucida Console'>(</font>
        database<font color='#5555FF'>&amp;</font> db,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> query
    <font face='Lucida Console'>)</font>
    <b>{</b>
        statement <font color='#BB00BB'>st</font><font face='Lucida Console'>(</font>db, query<font face='Lucida Console'>)</font>;
        st.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> st.<font color='#BB00BB'>get_num_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'><u>double</u></font> temp <font color='#5555FF'>=</font> st.<font color='#BB00BB'>get_column_as_double</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> temp;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>inline</font> <font color='#0000FF'><u>int</u></font> <b><a name='query_int'></a>query_int</b> <font face='Lucida Console'>(</font>
        database<font color='#5555FF'>&amp;</font> db,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> query
    <font face='Lucida Console'>)</font>
    <b>{</b>
        statement <font color='#BB00BB'>st</font><font face='Lucida Console'>(</font>db, query<font face='Lucida Console'>)</font>;
        st.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> st.<font color='#BB00BB'>get_num_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'><u>int</u></font> temp <font color='#5555FF'>=</font> st.<font color='#BB00BB'>get_column_as_int</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> temp;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>inline</font> int64 <b><a name='query_int64'></a>query_int64</b> <font face='Lucida Console'>(</font>
        database<font color='#5555FF'>&amp;</font> db,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> query
    <font face='Lucida Console'>)</font>
    <b>{</b>
        statement <font color='#BB00BB'>st</font><font face='Lucida Console'>(</font>db, query<font face='Lucida Console'>)</font>;
        st.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> st.<font color='#BB00BB'>get_num_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
        <b>{</b>
            int64 temp <font color='#5555FF'>=</font> st.<font color='#BB00BB'>get_column_as_int64</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> temp;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>inline</font> <font color='#0000FF'>const</font> std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font> <b><a name='query_blob'></a>query_blob</b> <font face='Lucida Console'>(</font>
        database<font color='#5555FF'>&amp;</font> db,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> query
    <font face='Lucida Console'>)</font>
    <b>{</b>
        statement <font color='#BB00BB'>st</font><font face='Lucida Console'>(</font>db, query<font face='Lucida Console'>)</font>;
        st.<font color='#BB00BB'>exec</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> st.<font color='#BB00BB'>get_num_columns</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>const</font> std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> temp <font color='#5555FF'>=</font> st.<font color='#BB00BB'>get_column_as_blob</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>st.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> temp;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>sqlite_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>query doesn't result in exactly 1 element</font>"<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_SQLiTE_TOOLS_H_
</font>

</pre></body></html>